local skynet = require "skynet"

local CMD = {}

local pools = {}

local function iterator()
    local i=0
    return function ()
        i = i % #pools + 1
        return i
    end
end
local iter = iterator()

function CMD.start(agents)
    agents = agents or 8
    for i=1,agents do
        pools[#pools+1] = skynet.newservice("agent")
    end
    skynet.retpack()
end

function CMD.fetch()
    local addr = pools[iter()]
    skynet.retpack(addr)
end

skynet.start(function ()
    skynet.dispatch("lua", function(_,_,cmd,...)
        local func = assert(CMD[cmd])
        func(...)
    end)
end)
